<template>
  <div class="water-chart">
    <div class="chart-title">{{ title }}</div>
    <div class="water-container">
      <div class="water-tank">
        <div 
          class="water-level" 
          :style="{ 
            height: waterLevel + '%',
            backgroundColor: waterColor,
            boxShadow: `0 0 20px ${waterColor}`
          }"
        >
          <div class="water-wave"></div>
        </div>
        <div class="water-percentage">{{ waterLevel }}%</div>
      </div>
      <div class="water-info">
        <div class="info-item" v-for="item in infoData" :key="item.label">
          <span class="info-label">{{ item.label }}</span>
          <span class="info-value">{{ item.value }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'WaterChart',
  props: {
    title: {
      type: String,
      default: '水位图'
    },
    waterLevel: {
      type: Number,
      default: 75
    },
    waterColor: {
      type: String,
      default: '#00d4ff'
    },
    infoData: {
      type: Array,
      default: () => [
        { label: '当前水位', value: '75%' },
        { label: '警戒水位', value: '90%' },
        { label: '安全水位', value: '60%' }
      ]
    }
  },
  mounted() {
    this.startWaveAnimation()
    this.startLevelAnimation()
  },
  beforeDestroy() {
    if (this.levelTimer) {
      clearInterval(this.levelTimer)
    }
  },
  methods: {
    startWaveAnimation() {
      const wave = this.$el.querySelector('.water-wave')
      if (wave) {
        setInterval(() => {
          wave.style.animation = 'none'
          setTimeout(() => {
            wave.style.animation = 'wave 2s ease-in-out infinite'
          }, 10)
        }, 2000)
      }
    },
    startLevelAnimation() {
      // 模拟水位变化
      this.levelTimer = setInterval(() => {
        const currentLevel = this.waterLevel
        const change = (Math.random() - 0.5) * 10
        const newLevel = Math.max(0, Math.min(100, currentLevel + change))
        
        // 更新水位
        this.$emit('update:waterLevel', newLevel)
        
        // 更新信息数据
        this.infoData = [
          { label: '当前水位', value: `${Math.round(newLevel)}%` },
          { label: '警戒水位', value: '90%' },
          { label: '安全水位', value: '60%' }
        ]
      }, 4000)
    }
  }
}
</script>

<style scoped>
.water-chart {
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
}

.chart-title {
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
  text-align: center;
}

.water-container {
  display: flex;
  align-items: center;
  gap: 20px;
  height: calc(100% - 60px);
}

.water-tank {
  position: relative;
  width: 80px;
  height: 200px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.05);
}

.water-level {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  transition: height 0.5s ease;
  border-radius: 8px 8px 0 0;
}

.water-wave {
  position: absolute;
  top: -10px;
  left: 0;
  right: 0;
  height: 20px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  animation: wave 2s ease-in-out infinite;
}

.water-percentage {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  font-size: 14px;
  font-weight: bold;
  text-shadow: 0 0 10px rgba(0, 0, 0, 0.8);
}

.water-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 6px;
}

.info-label {
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
}

.info-value {
  color: #00d4ff;
  font-size: 12px;
  font-weight: bold;
}

@keyframes wave {
  0%, 100% {
    transform: translateX(-100%);
  }
  50% {
    transform: translateX(100%);
  }
}
</style>